import axios from 'axios'
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: [],
    pjlist: [],
    detaillist: [],
    carlist: [],
  },
  getters: {
    getDetail(state) {
      return (id) => {
        return state.list.filter((item) => item.id === id)[0]
      }
    },
  },
  mutations: {
    SET_CARLIST(state, data) {
      state.list = data
    },
    SET_PJLIST(state, data) {
      state.pjlist = data
    },
    SET_DETAIL(state, data) {
      state.detaillist = data
    },
  },
  actions: {
    set_carlist(context) {
      axios.get('/api/getlist').then((res) => {
        context.commit('SET_CARLIST', res)
      })
    },
    set_pjlist(context) {
      axios.get('/api/pjlist').then((res) => {
        context.commit('SET_PJLIST', res)
      })
    },
    set_detaillist(context) {
      axios.get('/api/carlist').then((res) => {
        context.commit('SET_DETAIL', res)
      })
    },
  },
  modules: {},
  plugins: [
    createPersistedState({
      storage: window.sessionStorage, // 修改存储的状态
    }),
  ], // 状态持久化
})
